package MVC;

/**
 * View
 * @author deathbyaclown/André Uhres
 */
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
 
public class WindViewer extends JFrame implements Observer {
 
    private WindController controller;
    private JLabel direction;
    private JLabel speed;
    private JPanel buttonPanel;
    private JPanel mainPanel;
 
    public WindViewer(WindController controller) {
        super("WindViewer");
        this.controller = controller;
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        init();
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
    }
 
    private void init() {
        buttonPanel = new JPanel();
        mainPanel = new JPanel();
        direction = new JLabel();
        speed = new JLabel();
        mainPanel.add(new JLabel("Direction: "));
        mainPanel.add(direction);
        mainPanel.add(new JLabel("Speed: "));
        mainPanel.add(speed);
        getContentPane().add(mainPanel);
 
        JButton button = new JButton("Change Direction");
        button.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent event) {
                controller.changeDirection();
            }
        });
        buttonPanel.add(button);
 
        button = new JButton("Change Speed");
        button.addActionListener(new ActionListener() {
 
            public void actionPerformed(ActionEvent event) {
                controller.changeSpeed();
            }
        });
        buttonPanel.add(button);
        getContentPane().add(buttonPanel, BorderLayout.SOUTH);
    }
 
    /*
     * (non-Javadoc)
     *
     * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
     */
    public void update(Observable arg0, Object arg1) {
        Wind wind = (Wind) arg1;
        direction.setText(wind.getDir().toString());
        speed.setText(String.valueOf(wind.getSpeed()));
    }
}
